package myapp.framelibrary.skin.attrs;

import android.annotation.TargetApi;
import android.content.res.ColorStateList;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import myapp.framelibrary.skin.SkinManager;
import myapp.framelibrary.skin.SkinResource;

/**
 * Created by 超 on 2017/7/4.
 */

public enum SkinType {

    Text_COLOR("textColor") {
        @Override
        public void skin(View view, String mResName) {
            SkinResource skinResource = getSkinResource();
            ColorStateList colorByName = skinResource.getColorByName(mResName);
            if (colorByName == null) {
                return;
            }
            TextView textView = (TextView) view;
            textView.setTextColor(colorByName);
        }
    }, BACKGROUND("background") {
        @Override
        public void skin(View view, String mResName) {
            Drawable drawableByName = getSkinResource().getDrawableByName(mResName);
            if (drawableByName != null) {
                ImageView imageView = (ImageView) view;
                imageView.setBackgroundDrawable(drawableByName);
                return;
            }
            SkinResource skinResource = getSkinResource();
            ColorStateList colorByName = skinResource.getColorByName(mResName);
            if (colorByName != null) {
                ImageView imageView = (ImageView) view;
                imageView.setBackgroundColor(colorByName.getDefaultColor());
                return;
            }

        }
    }, SRC("src") {
        @Override
        public void skin(View view, String mResName) {
            Drawable drawableByName = getSkinResource().getDrawableByName(mResName);
            if (drawableByName != null) {
                ImageView imageView = (ImageView) view;
                imageView.setImageDrawable(drawableByName);
                return;
            }

        }
    };

    private String mResName;
    private String resName;

    SkinType(String resName) {
        this.mResName = resName;
    }


    public abstract void skin(View view, String mResName);

    public String getResName() {
        return resName;
    }

    public SkinResource getSkinResource() {
        return SkinManager.getInstance().getSkinResource();
    }
}
